const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

module.exports = {
  formatTime
}
/**
 * 通用登录态工具
 * 功能：判断登录态、处理未登录逻辑
 */
const isLogin = () => {
  const user = wx.getStorageSync('user');
  return !!user; // 有用户信息则返回 true，否则 false
};

const checkLogin = (onLogin, onNotLogin) => {
  if (isLogin()) {
    onLogin && onLogin(); // 已登录，执行登录后逻辑
  } else {
    // 未登录，弹窗提示并跳转
    wx.showModal({
      title: '未登录',
      content: '请先登录~',
      confirmText: '去登录',
      cancelText: '取消',
      success(res) {
        if (res.confirm) {
          wx.navigateTo({ url: '/pages/login/login' }); // 跳登录页
        } else {
          onNotLogin && onNotLogin(); // 取消时的逻辑
        }
      }
    });
  }
};

module.exports = {
  isLogin,
  checkLogin
};